<template>
    <div class="weiMusicContent">
        <div class="goBack" @click="goBack">
            <van-icon name="arrow-left" size="30" color="#0d0d0d" />
        </div>
        <div class="searchBox" v-if="!showSearchBox" >
            <van-icon name="search" size="30" color="#0d0d0d" @click="showSearchBox=true" />
        </div>
        <div class="searchBoxMain" v-if="showSearchBox">
            <van-search
                class="transparent-round-search"
                v-model="searchQuery"
                @keyup="goSearch"
                shape="round"
                ref="searchRef"
                maxlength="30"
                placeholder="请输入关键词搜索"
                @blur="searchBlur"
                @clear="clearSearchQuery"
            />
        </div>
        <div class="weiMusicBg">
            <img :src="imgBg" alt="">
        </div>
        <div class="weiMusicContentAll" @click="playAll">
            <SvgIcon width="20px" height="20px" name="pause" color="#a49144"></SvgIcon>
            <p>开始播放全部</p>
        </div>
        <div class="weiMusicListBox" v-if="musicList && musicList.length">
            <div class="weiMusicListBoxItem" v-for="(item, index) in musicList" :key="item.id" @click="playItem(item)">
                <div class="weiMusicListBoxItemLeft">
                    <SvgIcon width="30px" height="30px" name="trophyGold" color="#a49144" v-if="item.id=='1'"></SvgIcon>
                    <SvgIcon width="30px" height="30px" name="trophySilver" color="#a49144" v-else-if="item.id=='2'"></SvgIcon>
                    <SvgIcon width="30px" height="30px" name="trophyCopper" color="#a49144" v-else-if="item.id=='3'"></SvgIcon>
                    <p v-else>{{ item.id }}</p>
                </div>
                <div class="weiMusicListBoxItemRight">
                    <div class="weiMusicListBoxItemRightTop">
                        <p v-if="searchQuery == ''">{{ item.title }}</p>
                        <p v-else v-html="seachNews(item.title)"></p>
                    </div>
                    <div class="weiMusicListBoxItemRightBottom">
                        <p v-if="searchQuery == ''">{{ item.author }}</p>
                        <p v-else v-html="seachNews(item.author)"></p>
                    </div>
                </div>
            </div>
            <div class="weiMusicListBoxLast" v-if="musicList.length > 6">
                <p style="margin-bottom: 10px;">{{ `没有更多了` }}</p>
                <p>
                    <span>{{ '更多音乐' }}</span>
                    <a style="font-size: 12px;color: #d32d31;text-decoration:none;margin-left: 5px;" href="https://music.163.com/">网易云音乐</a>
                    <a style="font-size: 12px;color: #62bf82;text-decoration:none;margin-left: 5px;" href="https://y.qq.com/">QQ音乐</a>
                </p>
            </div>
        </div>
        <div class="emptyBox" v-else>
            <SvgIcon width="30px" height="30px" name="noMusic" color="#a49144"></SvgIcon>
            <p>{{ `歌单中没有搜索到 ${searchQuery}` }}</p>
            <p style="position: fixed;bottom: 30px;">
                <span style="font-size: 12px;">{{ '更多音乐' }}</span>
                <a style="font-size: 12px;color: #d32d31;text-decoration:none;margin-left: 5px;" href="https://music.163.com/">网易云音乐</a>
                <a style="font-size: 12px;color: #62bf82;text-decoration:none;margin-left: 5px;" href="https://y.qq.com/">QQ音乐</a>
            </p>
        </div>
    </div>
</template>
    
<script setup lang="ts">
import { ref, onMounted, computed, onUnmounted, onBeforeUnmount, watch, nextTick } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { sourceHeader } from '@/utils/plugins'
import { showToast, showFailToast } from 'vant'
import useSettingStore from '@/store/modules/setting'
import musicStore from '@/store/modules/musicDataList'
const settings = useSettingStore()
const musicStoreList = musicStore()

import imgBg from '@/assets/img/music_bg.jpg'

const route = useRoute()
const router = useRouter()


const musicList = ref<any>([])
const searchQuery = ref<string>('')
const showSearchBox = ref<boolean>(false)
const searchRef = ref<any>(null)


const seachNews = (contentText: any) => {
    if (searchQuery.value !== '' && contentText.indexOf(searchQuery.value) !== -1) {
        console.log('searchQuery.value', searchQuery.value)
		return contentText.replace(searchQuery.value, `<font style="color: #97fc82;">${searchQuery.value}</font>`)
	}
	return contentText
}

const playAll = () => {
    router.push({
        path: '/weiMusic/weiMusicItem',
    })
}

const playItem = (item: any) => {
    console.log('item', item)
    musicStoreList.handlerSetmusicItemDataList(item)
    router.push({
        path: '/weiMusic/weiMusicItem',
    })
}

const goBack = () => {
    router.go(-1)
}

onMounted(() => {
    settings.handlerLoadingShow(true)
    settings.handlerHeaderShow(false)
    if (musicStoreList.musicDataList && musicStoreList.musicDataList.length) {
        musicList.value = musicStoreList.musicDataList
        settings.handlerLoadingShow(false)
    }
});

onBeforeUnmount(() => {
    settings.handlerHeaderShow(true)
});


onUnmounted(() => {
});

const searchBlur = () => {
    if (searchQuery.value && searchQuery.value != '' && musicList.value && musicList.value.length > 0) {
        return
    }
    if (searchQuery.value && searchQuery.value != '' && musicList.value && musicList.value.length == 0) {
        // showToast('歌单中没有搜索到')
        return
    }
    showSearchBox.value = false
    searchQuery.value = ''
    if (musicStoreList.musicDataList && musicStoreList.musicDataList.length) {
        musicList.value = musicStoreList.musicDataList
        settings.handlerLoadingShow(false)
    }
}

const clearSearchQuery = () => {
    searchQuery.value = ''
    if (musicStoreList.musicDataList && musicStoreList.musicDataList.length) {
        musicList.value = musicStoreList.musicDataList
        settings.handlerLoadingShow(false)
    }
}

const goSearch = () => {
    if (showSearchBox) {
        settings.handlerLoadingShow(true)
        if (searchQuery.value == '' || !searchQuery.value) {
            if (musicStoreList.musicDataList && musicStoreList.musicDataList.length) {
                musicList.value = musicStoreList.musicDataList
                settings.handlerLoadingShow(false)
            }
        } else {
            // musicList.value = musicStoreList.musicDataList.filter((item: any) => item.title.includes(searchQuery.value))
            // if (musicList.value.length == 0) {
            //     musicList.value = musicStoreList.musicDataList.filter((item: any) => item.author.includes(searchQuery.value))
            // }
            musicList.value = musicStoreList.musicDataList.filter((item: any) => item.title.includes(searchQuery.value) || item.author.includes(searchQuery.value))
            settings.handlerLoadingShow(false)
        }
    }
}

watch([() => searchRef.value, () => searchQuery.value], (newVal) => {
    // console.log('新', newVal);
    if (searchRef.value) {
        nextTick(() => {
            searchRef.value?.focus()
        })
    } else {
        searchBlur()
    }
    if (newVal[1]) {
        goSearch()
    }
})

</script>


<style lang="scss" scoped>
.weiMusicContent {
    width: 100%;
    height: 100vh;
    background-color: #0d0d0d;
    position: relative;
    // padding: 15px;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    align-items: center;
    .goBack {
        position: absolute;
        top: 15px;
        left: 15px;
    }
    .searchBox {
        position: absolute;
        top: 15px;
        right: 15px;
    }
    .searchBoxMain {
        position: absolute;
        top: 0px;
        right: 0px;
        .transparent-round-search {
            background-color: transparent;
            border-radius: 16px;
            color: #FFFFFF; /* 文字颜色 */
            :deep(.van-search__content) {
                background-color: #0d0d0d;
                color: #666;
            }
            :deep(.van-field__control) {
                color: #FFFFFF; /* 文字颜色 */
            }
            ::placeholder { /* 使用 ::placeholder 选择器 */
                background-color: #0d0d0d;
                color: #666;
                opacity: 1; /* Firefox 中需要设置 opacity 为 1 */
            }
            input:-moz-placeholder { /* Firefox 的旧版本 */
                background-color: #0d0d0d;
                color: #666;
                opacity: 1;
            }
            input::-moz-placeholder { /* Firefox 的新版本*/
                background-color: #0d0d0d;
                color: #666;
                opacity: 1;
            }
            input:-ms-input-placeholder { /* IE 的老版本 */
                background-color: #0d0d0d;
                color: #666;
            }
            input::-webkit-input-placeholder { /* Chrome/Opera/Safari */
                background-color: #0d0d0d;
                color: #666;
            }
        }
    }
    .weiMusicBg {
        width: 100%;
        height: auto;
        img {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }
    }
    .weiMusicContentAll {
        min-width: 140px;
        height: auto;
        border-radius: 32px;
        border: 2px solid #a49144;
        display: flex;
        flex-direction: row;
        justify-content: center;
        align-items: center;
        padding: 10px;
        p {
            font-size: 14px;
            color: #a49144;
            margin-left: 5px;
        }
    }
    .weiMusicListBox {
        width: 100%;
        height: auto;
        // height: 55vh;
        overflow-y: scroll;
        margin-top: 30px;
        margin-bottom: 40px;
        .weiMusicListBoxItem {
            width: 100%;
            min-height: 80px;
            display: flex;
            flex-direction: row;
            align-items: center;
            // padding: 10px 20px;
            .weiMusicListBoxItemLeft {
                width: 20%;
                height: 100%;
                display: flex;
                flex-direction: row;
                align-items: center;
                justify-content: center;
                p {
                    font-size: 20px;
                    font-weight: 500;
                    color: #a49144;
                }
            }
            .weiMusicListBoxItemRight {
                width: 80%;
                height: 100%;
                display: flex;
                flex-direction: column;
                .weiMusicListBoxItemRightTop {
                    margin-bottom: 5px;
                    p {
                        font-size: 16px;
                        font-weight: 500;
                        color: #FFFFFF;
                        white-space: normal; // 默认处理方式，文本会根据容器宽度自动换行。
                        word-wrap: break-word; // 允许在单词内断行，防止长字符串溢出容器。
                    }
                }
                .weiMusicListBoxItemRightBottom {
                    p {
                        font-size: 14px;
                        color: #666666;
                        white-space: normal; // 默认处理方式，文本会根据容器宽度自动换行。
                        word-wrap: break-word; // 允许在单词内断行，防止长字符串溢出容器。
                    }
                }
            }
        }
        .weiMusicListBoxLast {
            width: 100%;
            min-height: 80px;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: end;
            p {
                width: 100%;
                text-align: center;
                font-size: 12px;
                color: #666666;
                display: flex;
                flex-direction: row;
                align-items: center;
                justify-content: center;
            }
        }
    }
    .emptyBox {
        width: 100%;
        height: auto;
        min-height: 50vh;
        overflow-y: auto;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        color: #666666;
        p {
            width: 80%;
            text-align: center;
            margin-top: 15px;
            font-size: 14px;
            // font-weight: 500;
            // color: #FFFFFF;
            overflow: hidden;
            line-height:20px;
        }
    }
}
</style>